<?php
/**
 * 广告管理
 *
 * User: kaikaic
 * Date: 2018/4/22
 * Time: 2:26
 */

namespace app\admin\controller;

use app\admin\service\AdvService;
use controller\BasicAdmin;
use service\ConfigService;
use service\DataService;

use think\Db;

class Adv extends BasicAdmin {

    protected $table = 'SystemAdvSpot';

    protected $advTable = 'SystemAdv';

    protected $categoryKey = "doc";

    public function index() {
        $this->title = "广告位管理";

        return parent::_list($this->table);
    }

    /**
     * 新建-广告位
     */
    public function add() {
        $types = ConfigService::cfgAdvType();
        $appTypes = ConfigService::cfgAPPType();

        if ($this->request->isGet()) {
            return view('form', ['title' => '新建广告位', 'type_list'=>$types, 'app_type'=>$appTypes]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (($ids = $this->doInsertAction($data)) && !empty($ids)) {
                $post = ['adv_id' => $ids, 'create_by' => session('user.id')];
                if (DataService::save($this->table, $post, 'id') !== false) {
                    $this->success('添加成功！', '');
                }
            }

            $this->error('添加失败，请稍候再试！');
        }
    }

    /**
     * 编辑-广告位
     */
    public function edit() {
        $types = ConfigService::cfgAdvType();

        if ($this->request->isGet()) {
            $id = $this->request->get('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $appTypes = ConfigService::cfgAPPType();

            $advSpot = AdvService::getInstance()->find($id);
            if (empty($advSpot)) {
                $this->error('记录不存在，请重新操作!');
            }

            return view('form', ['title'=>'编辑广告位', 'vo'=>$advSpot, 'type_list'=>$types, 'app_type'=>$appTypes]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if (($ids = $this->doInsertAction($data)) && !empty($ids)) {
                $post = ['adv_id' => $ids, 'create_by' => session('user.id')];
                if (DataService::save($this->table, $post, 'id') !== false) {
                    $this->success('编辑成功！', '');
                }
            }

            $this->error('编辑失败，请稍候再试！');
        }
    }

    /**
     * 删除-标签
     */
    public function del() {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 广告列表
     */
    public function ad() {
        $id = $this->request->get('id', '');
        if (empty($id)) {
            $this->error('参数错误，请稍候再试！');
        }

        $advSpot = AdvService::getInstance()->find($id);
        if (empty($advSpot)) {
            $this->error('记录不存在，请重新操作!');
        }

        $advList = AdvService::getInstance()->listAdvBySpot($id);
        for ($i=0; $i<count($advList); $i++) {
            $advList[$i]['spot_name'] = $advSpot['name'];
        }

        $this->assign("adv_list", $advList);
        $this->assign("spot", $advSpot);
        $this->assign("title", "广告列表");

        return view();
    }

    /**
     * 添加-广告
     */
    public function addAd() {
        $id = $this->request->get('id', '');
        if (empty($id)) {
            $id = $this->request->post('spot_id', '');

            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }
        }

        $advSpot = AdvService::getInstance()->find($id);
        if (empty($advSpot)) {
            $this->error('记录不存在，请重新操作!');
        }

        if ($this->request->isGet()) {
            $defaultImage = ConfigService::defaultImage();

            return view('ad_form', ['title' => '新建广告', 'image'=>$defaultImage, 'spot'=>$advSpot]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if (($ids = $this->doInsertAdAction($data)) && !empty($ids)) {
                $post = ['adv_id' => $ids, 'create_by' => session('user.id')];
                if (DataService::save($this->table, $post, 'id') !== false) {
                    $this->success('添加成功！', '');
                }
            }

            $this->error('添加失败，请稍候再试！');
        }
    }

    /**
     * 编辑-广告
     */
    public function editAd() {
        $id = $this->request->get('id', '');
        if (empty($id)) {
            $id = $this->request->post('id', '');

            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }
        }

        $adv = AdvService::getInstance()->findAd($id);
        if (empty($adv)) {
            $this->error('记录不存在，请重新操作!');
        }

        // 广告位信息
        $advSpot = AdvService::getInstance()->find($adv['spot_id']);

        if ($this->request->isGet()) {
            $defaultImage = ConfigService::defaultImage();

            return view('ad_form', ['title' => '编辑广告', 'image'=>$defaultImage, 'vo'=>$adv, 'spot'=>$advSpot]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();

            if (($ids = $this->doInsertAdAction($data)) && !empty($ids)) {
                $post = ['adv_id' => $ids, 'create_by' => session('user.id')];
                if (DataService::save($this->table, $post, 'id') !== false) {
                    $this->success('添加成功！', '');
                }
            }

            $this->error('添加失败，请稍候再试！');
        }
    }

    /**
     * 删除-广告
     */
    public function delAd() {
        if (DataService::update($this->advTable)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 数据写入
     */
    protected function doInsertAdAction($data, $ids = []) {
        if (empty($data['id'])) {
            $data['create_time'] = time();

            $result = $id = Db::name($this->advTable)->insertGetId($data);
        } else {
            $id = intval($data['id']);
            $result = Db::name($this->advTable)->where('id', $id)->update($data);
        }
        if ($result !== FALSE) {
            $ids[] = $id;
        }

        return join(',', $ids);
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data, $ids = []) {
        if (empty($data['id'])) {
            $data['status'] = 1;
            $data['create_time'] = time();

            $result = $id = Db::name($this->table)->insertGetId($data);
        } else {
            $id = intval($data['id']);
            $result = Db::name($this->table)->where('id', $id)->update($data);
        }
        if ($result !== FALSE) {
            $ids[] = $id;
        }

        return join(',', $ids);
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);

        $typeConfig = ConfigService::cfgAdvType();
        $appTypes = ConfigService::cfgAPPType();
        $cfgStatus = ConfigService::cfgAdvStatus();

        for ($i=0; $i<$size; $i++) {
            $data[$i]['status_txt'] = $cfgStatus[$data[$i]['status']];

            $data[$i]['type_txt'] = "";
            if (isset($typeConfig[$data[$i]['type']])) {
                $data[$i]['type_txt'] = $typeConfig[$data[$i]['type']];
            }

            $data[$i]['type_app_txt'] = "";
            if (isset($appTypes[$data[$i]['type_app']])) {
                $data[$i]['type_app_txt'] = $appTypes[$data[$i]['type_app']];
            }
        }

        $this->assign('cfg_status', $cfgStatus);
        $this->assign("type_list", $typeConfig);
        $this->assign("type_app_list", $appTypes);
    }
}